package com.gm.wholesale.module.setting.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;
import com.gm.commonlib.module.evenbus.EventBusUtils;
import com.gm.commonlib.module.evenbus.EventMessage;
import com.gm.commonlib.views.dialog.loadingDialog.LoadingDialog;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.bluetooth.BluetoothUtil;
import com.gm.wholesale.bluetooth.DeviceManger;
import com.gm.wholesale.bluetooth.WeightBlueType;
import com.gm.wholesale.bluetooth.bean.DeviceBean;
import com.gm.wholesale.bluetooth.weightBlue.KHWeightBluetooth;
import com.gm.wholesale.bluetooth.weightBlue.WXLWeightBluetooth;
import com.gm.wholesale.module.evenbus.EvenBusUtil;
import com.gm.wholesale.module.evenbus.EventBusType;
import com.gm.wholesale.module.setting.view.BluetoothListView;
import com.gm.wholesale.util.ViewUtil;

public class WeightBlueListFragment extends BaseFragment {
    private BluetoothListView mWeighBlueDeviceView;
    private DeviceBean mChoiceDeviceBean;

    private boolean mIsToConnection;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mWeighBlueDeviceView = new BluetoothListView(getActivity());
        showWeighBlueDevices();
        return mWeighBlueDeviceView;

    }

    private void showWeighBlueDevices() {
        mWeighBlueDeviceView.showProgressBar();
        mWeighBlueDeviceView.showDevice(WeightBlueType.getWeightFilter(), getWeightBluetConDevice());
        mWeighBlueDeviceView.setOnItemClickListener(deviceBean -> {
            mChoiceDeviceBean = deviceBean;
            if (deviceBean.getState() == 1) {
                mIsToConnection=false;
                ToastUtil.showToast("已断开连接");
                KHWeightBluetooth.getBluetoothManger(getContext()).close();
                WXLWeightBluetooth.getBluetoothManger(getContext()).close();
                mWeighBlueDeviceView.notifyDataSetChanged("");
                DeviceManger.getDeviceManger().cleanWeightDevice();
            } else {
                mIsToConnection=true;
                EvenBusUtil.connectionWeightBlue(deviceBean);
            }

        });

    }

    private String getWeightBluetConDevice() {
        return BluetoothUtil.getConnectionWeightDevice(context);
    }

    @Override
    public void onReceiveEvent(EventMessage event) {
        LoadingDialog.newLoadingDialog().hid();
        super.onReceiveEvent(event);
        int type = event.getCode();

        if (type == EventBusType.CONNECTION_DEVICE_SUCCESS.getType()) {
            DeviceBean deviceBean= (DeviceBean) event.getData();
            DeviceManger.getDeviceManger().saveWeightDevice(deviceBean);
            mWeighBlueDeviceView.notifyDataSetChanged(mChoiceDeviceBean.getAddress());
        }

        else if (type == EventBusType.CONNECTION_DEVICE_FAIL.getType()
        && mIsToConnection){
            ViewUtil.showWeighError();
        }
    }
    @Override
    public void onDetach() {
        if (mWeighBlueDeviceView!=null){
            mWeighBlueDeviceView.stop();
        }
        super.onDetach();
    }

    @Override
    public void onDestroy() {
        KHWeightBluetooth.getBluetoothManger(getContext()).stopFindDevice(getContext());
        WXLWeightBluetooth.getBluetoothManger(getContext()).stopFindDevice(getContext());
        super.onDestroy();
    }
}
